#include "cp_pfcapture.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QDoubleSpinBox>
#include <QVBoxLayout>
#include <QGridLayout>
#include "nodeconfig.h"

CP_PFCapture::CP_PFCapture(QWidget *parent) :
    QWidget(parent)
{
    /*Camera Settings*/
    labelShutter = new QLabel(tr("Shutter"));
    labelShutter->setFixedWidth(60);
    spinShutter = new QDoubleSpinBox;
    spinShutter->setValue(PFCMU::DEFAULTSHUTTER);
    spinShutter->setMaximumWidth(100);
    spinShutter->setToolTip(tr("Shutter speed of camera. 0.01 is 10ms."));
    buttonShutter = new QPushButton(tr("Set Shutter"));
    buttonShutter->setEnabled(true);

    labelGain = new QLabel(tr("Gain"));
    labelGain->setFixedWidth(60);
    spinGain = new QDoubleSpinBox;
    spinGain->setValue(PFCMU::DEFAULTGAIN);
    spinGain->setMaximumWidth(100);
    buttonGain = new QPushButton(tr("Set Gain"));
    buttonGain->setEnabled(true);
    connect(buttonGain,SIGNAL(clicked()),SLOT(SetGainButtonClicked()));

    /*Capture Settings*/
    labelCaptureFrames = new QLabel(tr("Frames"));
    txtCaptureFrames = new QLineEdit();
    txtCaptureFrames->setMaximumWidth(150);

    labelCaptureFileName = new QLabel(tr("Save as"));
    txtCaptureFileName = new QLineEdit();
    txtCaptureFileName->setText(tr("oneshot.raw"));
    txtCaptureFileName->setMaximumWidth(150);

    buttonStartCapture = new QPushButton(tr("Start Capture"));
    buttonStartCapture->setMaximumWidth(100);

    layoutGCameraSetting = new QGridLayout;
    layoutGCameraSetting->setAlignment(Qt::AlignLeft);
    layoutGCameraSetting->setSpacing(2);
    layoutGCameraSetting->setMargin(2);
    layoutGCameraSetting->addWidget(labelShutter,0,0);
    layoutGCameraSetting->addWidget(spinShutter,0,1);
    layoutGCameraSetting->addWidget(buttonShutter,1,1);

    layoutGCameraSetting->addWidget(labelGain,3,0);
    layoutGCameraSetting->addWidget(spinGain,3,1);
    layoutGCameraSetting->addWidget(buttonGain,4,1);

//    layoutGCapture = new QGridLayout;
//    layoutGCapture->addWidget(labelCaptureFrames,0,0);
//    layoutGCapture->addWidget(txtCaptureFrames,1,0);
//    layoutGCapture->addWidget(labelCaptureFileName,3,0);
//    layoutGCapture->addWidget(txtCaptureFileName,4,0);

    layout = new QVBoxLayout;
    layout->addLayout(layoutGCameraSetting);
    //layout->addLayout(layoutGCapture);
    layout->addWidget(labelCaptureFrames);
    layout->addWidget(txtCaptureFrames);
    layout->addWidget(labelCaptureFileName);
    layout->addWidget(txtCaptureFileName);
    layout->addWidget(buttonStartCapture);

    connect(buttonStartCapture,SIGNAL(clicked()),SLOT(StartCaptureButtonClicked()));
    this->setLayout(layout);
    this->setFixedWidth(150);
}

void CP_PFCapture::SetGainButtonClicked()
{
    double newGain = spinGain->value();
    emit gainChanged(newGain);
}

void CP_PFCapture::StartCaptureButtonClicked()
{
    QString filename(txtCaptureFileName->text());
    QString frames(txtCaptureFrames->text());
    QStringList paraLst;
    paraLst.append(frames);
    paraLst.append(filename);
    emit startCapture(paraLst);
}
